#
# Classifier for Fetchmail broken messages
# Straight-ups are handled by standard, but some are not parseable.
# The common mistake is that there is a letter in the first number
# of Status.
package Mail::Bounce::Fetchmail;

use strict;
use vars qw(@ISA @patterns);

@ISA = qw(Mail::Bounce::Heuristic);

$patterns[0] = << 'EoF';
Reporting-MTA: dns; localhost

Final-Recipient: ${RECIP-rfc822; \S+}
Last-Attempt-Date: ${DATE-.*}
Action: failed
Status: ${-.}.0.0
Diagnostic-Code: ${DIAGNOSTIC-.*}
EoF

sub validate {
    my $self = shift;
    my $report = $self->parts(1); # note - first part ignored. this is second
    return 0 if !$report;
    my $body = $report->bodyhandle();
    return 0 if !$body;
    my $keywords = $self->patternmatch($report->bodyhandle->as_string,
				       @patterns);
    return defined($keywords);
}

    

sub recipients {
    my $self = shift;

    if (wantarray()) {
	my $res;
	my $report = $self->parts(1);
	die "No part 1" if !$report;
	my $body = $report->bodyhandle();
	die "No body" if !$body;
	my $keywords = $self->patternmatch($report->bodyhandle->as_string,
					   @patterns);
	if (defined($keywords)) {
	    $res = new Mail::Bounce::Address;
	    $res->type("permfail");
	    $res->action("failed");
	    if ($$keywords{DIAGNOSTIC} =~ /^unknown user/) {
		$res->status("5.1.1 (Unknown user)");
	    } else {
		$res->status("5.0.0");
		$res->diagnostic($$keywords{DIAGNOSTIC});
	    }
	    $res->final_recipient($$keywords{RECIP});
	} else {
	    die "Postfix: Unmatched pattern in recipients()\n";
	}
	return $res;
    } else {
	return 1;
    }
}
1;




